package handlers

import (
	"deliv-mate/cache"
	"deliv-mate/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CourierAdd(c *gin.Context) {
	cour_code := c.PostForm("cour_code")
	cour_name := c.PostForm("cour_name")
	cour_phone := c.PostForm("cour_phone")
	if cour_code == "" || cour_name == "" || cour_phone == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Courier{
		CourCode:  cour_code,
		CourName:  cour_name,
		CourPhone: cour_phone,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CourierOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_to := c.PostForm("order_to")
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	if order_code == "" || order_to == "" || pid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderCode: order_code,
		OrderTo:   order_to,
		Pid:       pid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CourierOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var courOrder []models.CourOrder
	redis_key := "courier_order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_to as OrderTo",
				"couriers.cour_code as CourCode",
				"couriers.cour_name as CourName",
				"couriers.cour_phone as CourPhone",
			).
			Joins("left join couriers on orders.pid=couriers.id").
			Offset(offset).Limit(size).Find(&courOrder)
		jsonData, _ := json.Marshal(courOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &courOrder)
	}
	var total int64
	models.DB.Model(&models.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  courOrder,
		"total": total,
	})
	return
}
func CourierOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var courOrder models.CourOrder
	redis_key := "courier_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_to as OrderTo",
				"couriers.cour_code as CourCode",
				"couriers.cour_name as CourName",
				"couriers.cour_phone as CourPhone",
			).
			Joins("left join couriers on orders.pid=couriers.id").
			Where("orders.id = ?", id).Find(&courOrder)
		jsonData, _ := json.Marshal(courOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &courOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": courOrder,
	})
	return
}
